package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.dao.core.AnswerRepository;
import net.toocruel.iqismart.dao.core.ArticleRepository;
import net.toocruel.iqismart.dao.core.CommentRepository;
import net.toocruel.iqismart.dao.core.SupportRepository;
import net.toocruel.iqismart.entity.core.Exchange;
import net.toocruel.iqismart.entity.core.Good;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.security.core.support.Result;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.service.dto.GoodsExchangeDTO;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping()
public class GoodsController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;


    @Autowired
    private SupportRepository supportRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private AnswerRepository answerRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private SupportService supportService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private ExchangeService exchangeService;

    @GetMapping("goods/{id}")
    public ModelAndView detail(@PathVariable Long id,
                               HttpServletRequest request){

        ModelAndView modelAndView = new ModelAndView(getTemplate("goods","detail"));
        Good good = goodService.findById(id);
        modelAndView.addObject("good",good);

        modelAndView.addObject("seoTitle",good.getName()+" - 商品详情 - "+Setting.SETTING.getWebsite().getName());

        refreshCurrentUser();

        return modelAndView;
    }

    @RequestMapping("goods/exchange")
    @ResponseBody
    public Result exchange(
            @Valid GoodsExchangeDTO goodsExchangeDTO,BindingResult bindingResult,
            HttpServletRequest request){

        goodsExchangeDTO.setUser(getCurrentUser());
        Result result = goodService.exchange(goodsExchangeDTO,bindingResult);

        refreshCurrentUser();

        return result;
    }


    @GetMapping("exchanges")
    public ModelAndView exchanges(
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "desc") String sortOrder,
                         HttpServletRequest request){

        ModelAndView modelAndView = new ModelAndView(getTemplate("goods","exchanges"));

        Page<Exchange> exchanges = exchangeService.findALl(getCurrentUser().getId(),
                new PageRequest(page-1,size,new Sort(Sort.Direction.fromString(sortOrder),sortBy)));

        modelAndView.addObject("exchanges",exchanges);

        modelAndView.addObject("seoTitle","我的兑换 - "+Setting.SETTING.getWebsite().getName());

        return modelAndView;
    }


}
